package com.example.combwyaq.mvp.presenter;

import com.example.combwyaq.App;
import com.example.combwyaq.db.DaoManager;
import com.example.combwyaq.entity.UserEntity;
import com.example.combwyaq.mvp.contract.UserDetailsContract;
import com.example.combwyaq.mvp.model.entity.BaseEntity;
import com.example.combwyaq.utils.LogUtils;
import com.example.combwyaq.visualinterface.user.entity.UpdateUserEntity;
import com.example.combwyaq.visualinterface.user.entity.UploadFileEntity;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class UserDetailsPresenter extends BasePresenter<UserDetailsContract.IUserDetailsModel, UserDetailsContract.IUserDetailsView> {

private String file;

@Inject
public UserDetailsPresenter(UserDetailsContract.IUserDetailsModel iUserDetailsModel, UserDetailsContract.IUserDetailsView iUserDetailsView) {
        super(iUserDetailsModel, iUserDetailsView);
        }

public void requestUserDetails(Map<String,Object> map){
        if (map == null)
        return;
        m.requestUser(map).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(this);
        }

public void requestUpdateUser(){
        Map<String,Object>map = new HashMap<>();
    UserEntity userEntity = null;
    try {
        userEntity = DaoManager.getInstance().getDaoSession().queryBuilder(UserEntity.class)
                .list().get(0);
    } catch (Exception e) {
        e.printStackTrace();
    }
        map.put("user_id",userEntity.user_id);
        map.put("user_img",file.substring(file.lastIndexOf("/")+1));
        map.put("user_integral",userEntity.user_integral);
        map.put("user_name",userEntity.user_name);
        map.put("user_phone",userEntity.user_phone);
        map.put("user_pwd",userEntity.user_pwd);
        map.put("user_sex",userEntity.user_sex);
        LogUtils.e(userEntity.toString());
        requestUpdateUser(map);
        }

    public void requestUpdateUser(Map<String,Object>map){
        if (map == null)
            return;
        m.updateUser(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void uploadHead(String file){
        if (file.isEmpty())
        return;
        this.file = file;
        Map<String,Object>map = new HashMap<>();
        map.put("file",file);
        m.uploadHead(map).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new UploadOb(this));
        }

    @Override
    public void onNext(@NonNull BaseEntity entity) {
        if (entity instanceof UpdateUserEntity){
        LogUtils.e("UserDetailsPresenter:UpdateUserEntity");
//            //更新数据成功
            UserEntity userEntity = null;
            try {
                 userEntity = DaoManager.getInstance().getDaoSession().queryBuilder(UserEntity.class)
                        .list().get(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Map<String,Object>map = new HashMap<>();
        map.put("user_id",userEntity.user_id);
        requestUserDetails(map);
        return;
        }
        v.refreshView(entity);
        }

private static class UploadOb implements Observer<UploadFileEntity>{

    private UserDetailsPresenter presenter;
    public UploadOb(UserDetailsPresenter presenter){
        this.presenter = presenter;
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        this.presenter.onSubscribe(d);
    }

    @Override
    public void onNext(@NonNull UploadFileEntity uploadFileEntity) {
        presenter.requestUpdateUser();
    }

    @Override
    public void onError(@NonNull Throwable e) {
        this.presenter.onError(e);
    }

    @Override
    public void onComplete() {
        this.presenter.onComplete();
    }
}
}
